package cn.cvs.web.controller;

import cn.cvs.biz.AuctionUserBiz;
import cn.cvs.biz.AuctionUserBizImpl;
import cn.cvs.pojo.AuctionUser;
import cn.cvs.utils.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
//自定义Spring MVC   Model(pojo)  View(显示视图)   Controller(控制器-servlet)
@WebServlet(name="AuctionUserServlet",urlPatterns = {"/user/AuctionUserServlet"})
public class AuctionUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println("user --- action/操作 (增删改查)");
        AuctionUserBiz userBiz = new AuctionUserBizImpl();
        PrintWriter out =resp.getWriter();

        String opr =req.getParameter("opr");
        if (null==opr || opr.equals("")){
            opr = "login";
        }

        try {
            if ("login".equals(opr)){
                AuctionUser params =new AuctionUser();
                String name =req.getParameter("userName"); //userName
                String pwd =req.getParameter("userPassword");//userPassword
                String userIsAdmin =req.getParameter("userIsAdmin");//userIsAdmin  -->null
                boolean _isAdmin=false;
                if (null !=userIsAdmin){
                    _isAdmin= userIsAdmin.equals("1"); //==true ?false
                }

                params.setUserName(name);
                params.setUserPassword(pwd);
                //如果 用户在登录页面选择的是 管理员登陆,但是他输入的号码却是 普通用户的账号,也是登录失败
                params.setUserIsAdmin(_isAdmin);  //false 普通用户登录

                AuctionUser loginUser= null;
                loginUser = userBiz.login(params);
                // params=  userdao.login(params);
                if (null == loginUser) {
                    // System.out.println("账密错误,登录失败");
                    //返回登录页面
                    out.print(" <script type=\"text/javascript\">" +
                            "        alert(\"登录失败\");" +
                            "        window.history.back();" +
                            "    </script>");
                }else {
                    //System.out.println(loginUser);
                    //去首页
                    req.getSession().setAttribute(Constant.LOGINUSER,loginUser);
                    req.getRequestDispatcher("/index.jsp").forward(req,resp); //  localhost:8080/ch06/index.jsp
                   // return "/index.jsp";
                }
            }

            else if("logout".equals(opr)){
                req.getSession().removeAttribute(Constant.LOGINUSER);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            out.flush();
            out.close();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
